/**
 *
 *   Copyright (C) 2011 by Wilk Maia
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *
 * File: linked_list.cpp
 * Author: Wilk Maia
 * E-mail: wilkmaia@gmail.com
 * Version: 1.0.0
 * Date: 14/02/2011
 * 
 * This file is under CreativeCommons Attribution CC BY License and MIT License
 * 
 */

#ifndef __LINKED_LIST_CPP__
#define __LINKED_LIST_CPP__

#include "linked_list.hpp"

/**
 * linkedList class methods
 */
linkedList::linkedList(const string& filePath)
{
	ifstream myFile( filePath.c_str() );
	string currentLine, buffer;
	linkedList* thisItem;
	int k, m;

	if( !myFile.is_open() )
	{
		cout << "\nErro ao carregar " << filePath;
		return;
	}

	cout << "\n\t\t -- Carregando arquivo com fonte de itens... --\n";
	for( k = m = 0; !myFile.eof(); k = m = 0 )
	{
		buffer.clear();
		currentLine.clear()

		getline( myFile, currentLine );
		
		k = currentLine.find_first_of( "#", 0 );
		buffer = currentLine.substr( 0, k );
		thisItem->setItemId( atoi( buffer.c_str() ) );

		buffer.clear();

		m = currentLine.find_first_of( "#", k+1 );
		buffer = currentLine.substr( k+1, m - k - 1 );
		thisItem->setItemName( buffer );

		thisItem->setNextItem( thisItem );
	}
	myFile.close();
	cout << "\n\t\t -- Arquivo com fonte de itens carregado com sucesso. --\n";
}
linkedList::~linkedList()
{
	this->clear();
}
void linkedList::clear()
{
	linkedList* buf;
	for( ; buf->getItemId(); this = buf )
	{
		buf = this->getNextItem();
		delete this;
	}
	delete buf;
}
int linkedList::getItemId()
{
	return this->itemId;
}
string linkedList::getItemName()
{
	return this->itemName;
}
linkedList* linkedList::getNextItem()
{
	return this->nextItem;
}
void linkedList::setItemId(const int &id)
{
	this->itemId = id;
}
void linkedList::setItemName(const string &name)
{
	this->itemName = name;
}
void linkedList::setNextItem(const linkedList *&next)
{
	this->nextItem = next;
}

#endif // __LINKED_LIST_CPP__